//
//  MQTabBar.swift
//  SinaWeiBo
//
//  Created by 毛强 on 16/1/11.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class MQTabBar: UITabBar {
    var composeButtonClickClouser: (()->())?
    
   override init(frame: CGRect) {
        super.init(frame: frame)
    
        addSubview(composeButton)
        backgroundImage = UIImage(named: "tabbar_background")
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    // MARK: - 添加composeButton
    private lazy var composeButton:UIButton = {
        let btn = UIButton()
        
        btn.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.sizeToFit()
        
        return btn
    }()
    
    // MARK: - 监听点击事件
    @objc private func composeButtonClick(){
        composeButtonClickClouser?()
    }
    
    // MARK: - layoutSubviews
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //设置composebtn的center
        composeButton.center.x = self.bounds.width * 0.5
        composeButton.center.y = self.bounds.height * 0.5
        
        let width = bounds.width * 0.2
        var index = 0
        
        for value in subviews{
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                value.frame.origin.x = width * CGFloat(index)
                value.frame.size.width = width
                
                if index == 1{
                    index++
                }
                index++
            }
        }
    }
}


